package com.hyh.music.controller;
import com.alibaba.fastjson.JSONObject;
import com.hyh.music.Result.Result;
import com.hyh.music.constant.JwtClaimsConstant;
import com.hyh.music.domain.domainDTO.AdminLoginDTO;
import com.hyh.music.domain.domains.Admin;
import com.hyh.music.domain.domianVO.AdminLoginVO;
import com.hyh.music.interceptor.JwtTokenAdminInterceptor;
import com.hyh.music.properties.JwtProperties;
import com.hyh.music.service.AdminService;
import com.hyh.music.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hyh
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/admin")
public class adminController {

    @Autowired
    AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result<AdminLoginVO> Login(@RequestBody AdminLoginDTO adminLoginDTO) {
        log.info("客户端登录{}", adminLoginDTO);
        Admin admin = adminService.login(adminLoginDTO);
        //登录成功生成JWT令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.ADMIN_ID, admin.getId());
        //根据密钥ttl和自定义载荷来得到token
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        AdminLoginVO adminLoginVO = AdminLoginVO.builder()
                .id(admin.getId())
                .name(admin.getName())
                .token(token)
                .build();
        return Result.success(adminLoginVO);
    }

    @GetMapping("/test")
    public Result test(){
        log.info("测试成功");
        return Result.success();
    }


}
